package com.example.testktp;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.kkh.tp.ISCChannel;
import com.kkh.tp.SCChannel;
import com.kkh.tp.SCTransport;
import com.kkh.tp.SCUdpChannel;

public class MainActivity extends Activity implements ISCChannel {
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		if(item.getItemId() == R.id.itemServer) {
			Intent intent = new Intent(this, TransServerActivity.class);
			
			startActivity(intent);
		}
		
		// TODO Auto-generated method stub
		return super.onMenuItemSelected(featureId, item);
		
		
			
	}

	static final String tag="main";
	
	SCChannel mCh, mUdpCh;
	MyTp mTp;
	
	
	SharedPreferences mPrep;
	
	// Views
	Button btnCnn;
	Button btnSend, btnUdpSend;
	EditText edRemoteIp;
	EditText edRemotePort;
	EditText edSendText;
	TextView tvRecvText;

	private ProgressBar pbConnect;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		mPrep = this.getSharedPreferences("com.example.testktp", Activity.MODE_PRIVATE);
		
		initViews();
		
		mTp = new MyTp(this);
		mTp.start();
		mTp.command(mTp.CMD_WAIT);
		mTp.command(mTp.CMD_TRANS);
		mTp.commanSync(mTp.CMD_SUSPEND);
		mTp.command(mTp.CMD_WAIT);
		
		
		mCh = new SCChannel(mTp);
		mCh.setOnListener(this);
		
		mUdpCh = new SCUdpChannel(mTp);
		mUdpCh.setOnListener(this);
		mUdpCh.requestBind(5002);
		mUdpCh.requestConnect("192.168.0.91", 5001);

	}

	private void initViews() {
		// TODO Auto-generated method stub
		
		edRemoteIp = (EditText)findViewById(R.id.edRemoteIp);
		edRemotePort = (EditText)findViewById(R.id.edRemotePort);
		String dip = mPrep.getString("remoteIp", "");
		String ports = mPrep.getString("remotePort", "0");
		edRemoteIp.setText(dip);
		edRemotePort.setText(ports);
		
		tvRecvText = (TextView)findViewById(R.id.tvRecvText);
		pbConnect = (ProgressBar)findViewById(R.id.pbConnect);
		pbConnect.setProgress(0);
		pbConnect.setIndeterminate(false);
		
		
		btnCnn = (Button)findViewById(R.id.btnCnnDis);
		btnCnn.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(mCh.getStatus() == SCTransport.STATUS_DISCONNECTED) {
					String dip = edRemoteIp.getText().toString();
					if( dip.indexOf("*") != -1) {
						dip = dip.replace("*",  ".");
						edRemoteIp.setText(dip);
					}
					String portstr = edRemotePort.getText().toString();
					mCh.requestConnect(dip, Integer.parseInt(portstr));
					setConnectingView();
					SharedPreferences.Editor edit = mPrep.edit();
					edit.putString("remoteIp", dip);
					edit.putString("remotePort", portstr);
					edit.commit();
				}	
				else {
					Log.d(tag, "disconnecting...");
					mCh.requestClose();
					setDisconnectView();
				}	
			}
		});
		
		
		
		btnSend = (Button)findViewById(R.id.btnSendText);
		btnSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				String text = edSendText.getText().toString();
				byte[] buf = new byte[10];
				for(int i=0;i<buf.length;i++) {
					buf[i] = 'a';
				}
				
				
				mCh.requestWrite(text.getBytes());
				mCh.requestWrite(buf);
			}
		});
		
		btnUdpSend = (Button)findViewById(R.id.btnUdpSend);
		btnUdpSend.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				String text = edSendText.getText().toString();
				
				String dip = edRemoteIp.getText().toString();
				if( dip.indexOf("*") != -1) {
					dip = dip.replace("*",  ".");
					edRemoteIp.setText(dip);
				}
				String portstr = edRemotePort.getText().toString();
				
				mUdpCh.requestWrite("udp....".getBytes());
				mUdpCh.requestWrite("udp2222....".getBytes());
				mUdpCh.requestWrite(text.getBytes());
			}
		});
		
		edSendText = (EditText)findViewById(R.id.edSendText);
		
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		Log.d(tag, "onDestroy....");
		mCh.requestClose();
		mTp.requestEnd();
	}

	@Override
	public void OnSCReceive(SCChannel c, byte[] packet, int count) {
		// TODO Auto-generated method stub
		if(c == mCh) {
			String str = new String(packet, 0, count);
			Log.d(tag, "== child recv = " + str);
			tvRecvText.setText(str);
		}
	}

	@Override
	public void OnSCStatusChanged(SCChannel c, int status) {
		// TODO Auto-generated method stub
		if(c == mCh) {
			if(status == SCTransport.STATUS_CONNECTED) {
				Log.d(tag, "connected...");
				setConnectedView();
			}
			else if(status == SCTransport.STATUS_DISCONNECTED) {
				Log.d(tag, "disconnected...");
				mCh.requestClose();
				setDisconnectView();
			}
		} else if(c == mUdpCh) {
			
		}
			
	}

	void setConnectingView() {
		btnCnn.setText("Disconnect");
		pbConnect.setIndeterminate(true);
	}
	
	void setDisconnectView() {
		btnCnn.setText("Connect");
		pbConnect.setIndeterminate(false);
		pbConnect.setProgress(0);
	}
	
	void setConnectedView() {
		btnCnn.setText("Disconnect");
		pbConnect.setIndeterminate(false);
		pbConnect.setProgress(100);
	}

}
